package com.zhbr.mvp.base.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModel;

import com.zhbr.mvp.base.presenter.BasePresenter;
import com.zhbr.mvp.base.view.IView;

import butterknife.Unbinder;

public abstract class BaseFragment<P extends BasePresenter> extends Fragment implements IView {

    protected P mPresenter;
    protected Context mContent;
    protected View mRootView;
    private Unbinder unbinder;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(getContentView(), container, false);
        getViewModel();
        initView(mRootView);
        initPresenter();
        mContent=getContext();
        initData();
        return mRootView;
    }



    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // TODO: Use the ViewModel
        initPresenter();
    }

    /**
     * 创建Presenter
     *
     * @return Presenter
     */
    protected abstract P createPresenter();

    public abstract void getViewModel();

    public abstract Context getActivityContext();
    public abstract int getContentView();

    public abstract void initView(View view);

    public abstract void initData();


    private void initPresenter() {
        mPresenter = createPresenter();
        // 完成Presenter和view的绑定
        if (mPresenter != null) {
            mPresenter.attachView(this);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // 将Presenter和view解绑
        if (mPresenter != null) {
            mPresenter.detachView();
            mPresenter = null;
        }
        // 解绑ButterKnife
        if (unbinder != null) {
            unbinder.unbind();
        }
    }


    public void showToast(String msg){
        Toast.makeText(mContent,msg,Toast.LENGTH_SHORT).show();
    }
    /**
     * 异步显示
     * @param msg
     */
    public void showToastSync(String msg){
        Looper.prepare();
        Toast.makeText(mContent,msg,Toast.LENGTH_SHORT).show();
        Looper.loop();
    }

    public void navigateTo(Class clazz){
        Intent intent = new Intent(mContent,clazz);
        startActivity(intent);
    }

    @Override
    public void showLoading() {
        // 这里实现自己的加载弹框
        Toast toast=Toast.makeText(mContent,"showLoading",Toast.LENGTH_SHORT    );
        toast.show();
    }

    @Override
    public void dismissLoading() {
        // 取消弹框
        Toast toast=Toast.makeText(mContent,"dismissLoading",Toast.LENGTH_SHORT    );
        toast.show();
    }

    @Override
    public void onFail(Throwable ex, String code, String msg) {
        // 基础的网络请求失败处理
        Log.d("网络请求", "onFail: 请求失败！");
    }

    @Override
    public void onNetError() {
        // 网络错误处理
        Log.d("网络请求", "onNetError: 网络异常！");
        showToast("网络异常！");

    }
}
